<?php
namespace app\admin\controller;
use app\service\billService;
use app\service\communityService;
use app\service\propertyService;
use invoice\Api;
use think\facade\Db;
use think\facade\View;

class CommunityZftGate extends Common{
    public function list(){
        $com = $this->pidTokenId();

        $data = request()->get();
        if (!empty($data)){
//            if(!empty($data['property_name'])){
//                $where['name'] = array("like","%".$data['property_name']."%");//账单编号
//            }
        }
        //$where['a.property_id'] = array('in',$com['property_id']);
        $where[] =['community_id','in',$com['com_id']];
        $where = array_filter($where);
        $gatelist =Db::name('community_zft_gate')
                        ->where($where)
                        ->order('id','desc')
                        ->paginate(20);


        $number = Db::name('community_zft_gate')->where($where)->count();
        View::assign('number',$number);
        if($number>0){
            foreach($gatelist as $lis){
                $propertyzftInfo= (new communityService())->getZftInfo($lis["community_id"]);
                //
                $lis["binding_alipay_logon_id"]=$propertyzftInfo["binding_alipay_logon_id"];
                ///$lis['property_id'] = (new communityService())->propertyIdName($lis['property_id']);
                $list[] = $lis;
            }
            View::assign('list', $list);
        }else{
            View::assign('list',$gatelist);
        }

        View::assign('gatelist',$gatelist);
        View::assign("appid", env('APP.mini_program_appid'));
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch("list");
    }
    
    
    public function add(){
         //根据当前账号查询物业信息
        $com = $this->pidTokenId();
        $where[] =['id','in',$com['com_id']];// array('in',$com['com_id']);
        $communitylist =Db::name('community')->where($where)->order('id desc')->select()->toArray();
        View::assign('communitylist',$communitylist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $communityinfo=Db::table('community')->where("id",$data["community_id"])->find();
            if(empty($communityinfo)){
             return "小区不存在";
            }
            $data["community_name"]=$communityinfo["community_name"];
            //将数据插入数据库
            $result = Db::name('community_zft_gate')->insert($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加成功',url('list'));
            }else{
                $this->error('添加失败',url('add'));
            }
        }else{
            return View::fetch('add');
        }
    }
    
    public function edit(){
         //根据当前账号查询物业信息
        $com = $this->pidTokenId();
        $where[] =['id','in',$com['com_id']];// array('in',$com['com_id']);
        $communitylist =Db::name('community')->where($where)->order('id desc')->select()->toArray();
        View::assign('communitylist',$communitylist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $communityinfo=Db::table('community')->where("id",$data["community_id"])->find();
            if(empty($communityinfo)){
                return "小区不存在";
            }
            $data["community_name"]=$communityinfo["community_name"];
            //将数据插入数据库
            $result = Db::name('community_zft_gate')->update($data);
            //判断数据是否插入成功
            if($result){
                $this->success('修改成功',url('gateList'));
            }else{
                $this->error('修改失败',url('gateList'));
            }
        }else{
            $id=request()->get("id");
            $info=Db::name('community_zft_gate')->where("id",$id)->find();
            View::assign("info",$info);


            return View::fetch('edit');
        }
    }
    



}